package com.redrose.rxandroiddemo.service;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.app.Notification;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

public class MyService extends AccessibilityService {


    public static final String TAG = "--MyService--";
    public static final String homeActivity = "com.xunmeng.merchant.ui.MainFrameTabActivity";
    public static final String viewPagerClassName = "androidx.viewpager.widget.ViewPager";


    private GestureDescription gestureDescription;

    public MyService() {
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType()) {
            String packageName = event.getPackageName().toString();
            String className = event.getClassName().toString();
            Log.d(TAG, "----1-----" + packageName);
            Log.d(TAG, "----2-----" + className);
            //被监听的界面
            if (homeActivity.equals(className)) {
                AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
                jiexiAccessibilityNodeInfo(rootInActiveWindow);
            }
        } else if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
            //获取Parcelable对象
            Parcelable data = event.getParcelableData();
            //判断是否是Notification对象
            if (data instanceof Notification) {
                Notification notification = (Notification) data;

            }

        }

    }

    @Override
    public void onInterrupt() {
        Log.d(TAG, "----onInterrupt-----");
    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.d(TAG, "----onServiceConnected-----");
    }

    public void jiexiAccessibilityNodeInfo(AccessibilityNodeInfo info) {
        String parentName = info.getClassName().toString();
        Log.d(TAG, "----parentName----- " + parentName);
        if (TextUtils.equals("android.widget.TextView", parentName)) {
            CharSequence text = info.getText();
            if (!TextUtils.isEmpty(text)) {
                String viewName = info.getText().toString();
                if (TextUtils.equals(viewName, "消息")) {
                    Log.d(TAG, "----消息1----- " + viewName);
                    AccessibilityNodeInfo parent = info.getParent();
                    parentName = parent.getClassName().toString();
                    Log.d(TAG, "----parentName1----- " + parentName);
                }
                Log.d(TAG, "----viewName----- " + viewName);
            }
        }
        int childCount = info.getChildCount();
        Log.d(TAG, "----childCount----- " + childCount);
        for (int i = 0; i < childCount; i++) {
            AccessibilityNodeInfo child = info.getChild(i);
            String childClassName = child.getClassName().toString();
            Log.d(TAG, "----childClassName----- " + childClassName);
            if (TextUtils.equals(viewPagerClassName, childClassName)) {
                AccessibilityNodeInfo bottomParentLayout = info.getChild(1);
                AccessibilityNodeInfo msgItem = bottomParentLayout.getChild(0).getChild(1);
                if (msgItem == null) {
                    continue;
                }
                CharSequence className = msgItem.getClassName();
                if (!TextUtils.isEmpty(className)) {
                    String itemLayoutClassName = msgItem.getClassName().toString();
                    if (TextUtils.equals("android.widget.RelativeLayout",itemLayoutClassName)) {
                        msgItem.setClickable(true);
                        msgItem.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
                        msgItem.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    }
                }
                break;
            }
            CharSequence text = child.getText();
            if (!TextUtils.isEmpty(text)) {
                String viewName = child.getText().toString();
                if (TextUtils.equals(viewName, "消息")) {
                    Log.d(TAG, "----消息2----- " + viewName);
                    AccessibilityNodeInfo parent = child.getParent();
                    parentName = parent.getClassName().toString();
                    Log.d(TAG, "----parentName2----- " + parentName);
                }
            }
            jiexiAccessibilityNodeInfo(child);
        }
    }
}
